#include "gamepushbutton.h"
#include <QPropertyAnimation>

GamePushButton::GamePushButton(QString m_imgNormal, QString m_imgPressed)
{
    this->m_imgNormal = m_imgNormal;
    this->m_imgPressed = m_imgPressed;
    QPixmap buttonPix;
    bool result = buttonPix.load(m_imgNormal);
    if(result)
    {
        QSize qSize(buttonPix.width(), buttonPix.height());
        this->setFixedSize(qSize);
        this->setStyleSheet("QPushButton{border:0px}");
        this->setIcon((buttonPix));
        this->setIconSize(qSize);
    }

}
//向上跳
void GamePushButton::zoomUp()
{
    QPropertyAnimation* animation = new QPropertyAnimation(this, "geometry");
    animation->setDuration(500);
    animation->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));
    animation->setEndValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
    animation->setEasingCurve(QEasingCurve::OutBounce);
    animation->start();
}

void GamePushButton::zoomDown()
{
    QPropertyAnimation* animation = new QPropertyAnimation(this, "geometry");
    animation->setDuration(500);
    animation->setStartValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
    animation->setEndValue(QRect(this->x(),this->y(),this->width(),this->height()));
    animation->setEasingCurve(QEasingCurve::OutBounce);
    animation->start();
}

void GamePushButton::mousePressEvent(QMouseEvent *event)
{
    if(m_imgPressed != "")
    {
        QPixmap pix;
        bool ret = pix.load(this->m_imgPressed);
        if(ret)
        {
            this->setFixedSize(pix.width(),pix.height());
            this->setStyleSheet("QPushButton{border:0px}");
            this->setIcon(pix);

        }
    }
    this->QPushButton::mousePressEvent(event);
}

void GamePushButton::mouseReleaseEvent(QMouseEvent *event)
{
    if(m_imgPressed != ""){
        QPixmap pix;
        bool ret = pix.load(this->m_imgNormal);
        if(ret){
            this->setFixedSize(pix.width(),pix.height());

            this->setStyleSheet("QPushButton{border:0px}");
            this->setIcon(pix);
            this->setIconSize(QSize(pix.width(),pix.height()));
        }
    }
    this->QPushButton::mouseReleaseEvent(event);
}
